package com.example.mock.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class TestController {

    @Autowired
    private WebApplicationContext applicationContext;

    @ResponseBody
    @RequestMapping(value = "/getAllUrl", method = RequestMethod.GET)
    public Object getAllUrl() throws IOException {
        StringBuffer bf = new StringBuffer();
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

        List<String[]> dataList = new ArrayList<>();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();

            Set<RequestMethod> methodSet = info.getMethodsCondition().getMethods();
            Set<String> patternsSet = info.getPatternsCondition().getPatterns();

            if (methodSet.size() > 0 && patternsSet.size() > 0) {
                String type = String.valueOf(methodSet.toArray()[0]);
                String url = String.valueOf(patternsSet.toArray()[0]);
                String className = method.getMethod().getDeclaringClass().getName();
                String methodName = method.getMethod().getName();

                dataList.add(new String[]{type, url, className, methodName});
            }
        }
        String[] headers = new String[]{"请求方式", "接口路径", "后端类", "后端方法名"};
        writeToCSV(dataList, "d://" + System.currentTimeMillis() + ".csv", headers);

        return dataList;
    }


    /**
     * 将数据写入CSV文件
     *
     * @param dataList 要写入的数据列表，每个元素是一个字符串数组，表示一行数据
     * @param filePath CSV文件的路径
     * @param headers  CSV文件的列标题，也是一个字符串数组
     * @throws IOException 如果在写入过程中发生文件写入错误
     */
    public static void writeToCSV(List<String[]> dataList, String filePath, String[] headers) throws IOException {
        try (FileWriter writer = new FileWriter(filePath)) {
            // 写入列标题
            writeRow(writer, headers);

            // 写入数据行
            for (String[] row : dataList) {
                writeRow(writer, row);
            }
        }
    }

    /**
     * 写入一行数据到CSV文件
     *
     * @param writer 文件写入器
     * @param row    要写入的一行数据，是一个字符串数组
     */
    private static void writeRow(FileWriter writer, String[] row) throws IOException {
        for (int i = 0; i < row.length; i++) {
            writer.write(row[i]);
            if (i < row.length - 1) {
                writer.write(",");
            }
        }
        writer.write("\n");
    }
}
